<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Verify;
class UserController extends Controller {
/**
用户列表方法
*/
    public function indexAction(){
        $Model = M();

        $user = $Model->query("select t1.username,t1.id,t1.mail,t2.group_name from user t1 left join groups t2 on t1.gid = t2.id");
//        var_dump($user);
        $this->assign('user',$user);
        $this->display('index');
    }

/**
编辑用户资料方法
*/
    public function changeAction(){

        $id = I('get.id');

        $Model = M();
//获得用户资料
        $userInfo = $Model->query("SELECT * FROM user WHERE id='{$id}'");
//获得职业
        $jobsInfo = $Model->query("select * from jobs");


        $userInfo = $userInfo[0];
        $this->assign('userInfo',$userInfo);
        $this->assign('jobsInfo',$jobsInfo);
        $this->display('change');
    }

/**
删除用户方法
*/
    public function deleteAction(){
        $id = I('get.id');
        var_dump($id);
    }


    /**
    添加用户方法
     */
    //取得数据库的用户组方法
    public function addAction(){
        $model = M();
        $groups = $model->query("SELECT * FROM groups");
        $this->assign('groups',$groups);
        $this->display('user_add');
    }
    //添加用户方法
    public function adduserAction(){

        $username = I('post.user_name');
        $mail = I('post.email','','email');
        $password = md5(I('post.password'));
        $gid = I('post.group_id');

        // 判断邮箱
        if (empty($mail)) $this->error('邮箱格式不正确','',2);

        // 判断名是否存在
        $Model = M();
        $isExist = $Model->query("SELECT * FROM user where username='{$username}'");
        if (!empty($isExist)) $this->error('用户已存在','',2);
        $sql = "insert into `user` (username,mail,password,gid) VALUES('{$username}','{$mail}','{$password}','{$gid}')";
        if($Model->execute($sql)){
            $this->success('添加成功',U('Admin/User/index'),2);
        } ;




    }

}

